package com.weifeng.jdsignin.jd;

import com.weifeng.jdsignin.jd.cookie.JdCookie;
import com.weifeng.jdsignin.jd.head.JdPlusSignInHead;
import com.weifeng.jdsignin.jd.poolcon.PoolCon;
import com.weifeng.jdsignin.log.ConfigLog;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

/**
 * @description：TODO
 * @USER ：weifeng
 * @date ：2022/11/11 下午 03:21
 */
public class JdPlusSignin extends ConfigLog {
    private static Logger logger = Logger.getLogger(JdPlusSignin.class);
    private static String url = "https://api.m.jd.com/api?client=iOS&clientVersion=11.1" +
            ".2&appid=jdchoujiang_h5&t=1658141998350&functionId=turncardChannelSign&body=%7B%22turnTableId%22%3A%221265%22%2C%22fp%22%3A%22%22%2C%22eid%22%3A%22%22%7D&h5st=20220718185958922%3B2883784253783887%3Bb342e%3Btk02w77b11b1e18nWTt65iIElAY9WjZ7EqQIMxHFp623olWDIR4IaxoOpUIbD%2B31azZW5KRFOMVYv0CelrO9cWXY9NWz%3B6d659f19880c99cf351d70fb831da43edab033f6cb1c60c300242370cf985f7e%3B3.1%3B1658141998922%3B7414c4e56278580a133b60b72a30beb2764d2e61c66a5620e8e838e06644d1bfdd835c52fec3b856e4c3514b12d473231f8336a29a4cc17eb97cb15291d598ae3ca11b1013bb2a6b697976b577a65e4422b2b13afe9082f855ca6aa08491f2ee28e3011e8e405b0c941df25f52bf57281f1266e1913c6bcccccd18c9992ad62b&uuid=b4dbf4a441022a816e0c371946c1af2d6d63a575&build=168158&osVersion=14.2.1&networkType=4g&partner=&d_brand=iPhone&d_model=iPhone13%2C2&turnTableId=1265&fp=&eid=";

    public static CloseableHttpResponse plusSignIn(String email, String pt_pin, String pwdt_id, String pt_key) throws IOException {
            CloseableHttpClient signin =
                    HttpClients.custom().setConnectionManager(PoolCon.poolCon()).build();
            HttpPost httpPost = new HttpPost(url);

            //设置请求头
            HashMap<String, String> headMap = JdPlusSignInHead.headMap();
            Iterator<String> header = headMap.keySet().iterator();
            while (header.hasNext()) {
                String key = header.next();
                httpPost.setHeader(key, headMap.get(key));
            }

            //从excel中取出参数  添加到请求参数中
            HashMap<String, String> cookieMap = JdCookie.cookieMap();

            logger.info("读取到的 email：" + email);
            cookieMap.put("pt_pin", pt_pin);
            logger.info("读取到的 pt_pin：" + pt_pin);

            cookieMap.put("pwdt_id", pwdt_id);
            logger.info("读取到的 getPwdt_id：" + pwdt_id);

            cookieMap.put("pt_key", pt_key);
            logger.info("读取到的 pt_key：" + pt_key);

            //把 map 转成string，但是 收尾 有 {}，先把 {} 去掉再放到cookie中
            String s = cookieMap.toString().replace(",",";");;
            httpPost.setHeader("cookie", s.substring(1, s.length() - 1));
            //请求
            CloseableHttpResponse execute = signin.execute(httpPost);

//            int code = execute.getStatusLine().getStatusCode();
//            //输出结果
//            logger.info(pt_pin+"的签到状态码："+code);
//            logger.info(pt_pin+"的签到结果是："+EntityUtils.toString(execute.getEntity()));
            return execute;
    }
}
